var classContrllers = angular.module('classContrllers', []);

classContrllers.controller('ClassCtrl',['$scope','$http',
                                            
  function($scope,$http){
	$http.get('/mvc/class/grade/listdata').success(function(data){
		$scope.grades=data;
	});

	$scope.pageIndex = 1;
	$scope.pageSize = 5;
	
	var param= {};
	param.pageIndex = $scope.pageIndex;
	param.pageSize = $scope.pageSize;
	
	$http(
			{
				method:'POST',
				url:'/mvc/class/listdata',
				data:$.param(param),
				headers : {'Content-Type':'application/x-www-form-urlencoded'}
			}).success(
				function(data, status, headers, config){
					$scope.ClassList=data.details;
					$scope.pageIndex = data.pageIndex;
					$scope.itemsLength = data.count;
				}
			);
	
	
	$scope.formData={};
	
	$scope.newClass = function(){
		$scope.edit_title = "New Class: ";
		$scope.isEditing = true;
		$scope.formData={};
	};
	
	$scope.edit = function(cls){
		$scope.edit_title = "Edit Class: ";
		$scope.isEditing = true;
		$scope.formData.id = cls.id;
		$scope.formData.name = cls.name;
		$scope.formData.nurseryId = cls.nurseryId;
		$scope.formData.gradeId = cls.grade.id;
	};
	
	$scope.update = function(data){
		
		if(data.name == null || data.name == ""){
			alert("Please input the department name.");
			return;
		}
		
		if(data.gradeId == null){
			alert("Please select a grade.");
			return;
		}
		
		if(data.id == null){
			data.id = "";
		}
		
		var param = {};
		param.id = data.id;
		param.name = data.name;
		param.gradeTypeId = data.gradeId;
		param.pageIndex = $scope.pageIndex;
		param.pageSize = $scope.pageSize;
		
		$http(
		{
			method:'POST',
			url:'/mvc/class/update',
			data:$.param(param),
			headers : {'Content-Type':'application/x-www-form-urlencoded'}
		}).success(
			function(data, status, headers, config){
				$scope.ClassList=data.details;
				$scope.itemsLength = data.count;
				$scope.isEditing = false;
			}
		);
	};
	
	
	$scope.remove = function(cls){
		if(confirm("Do you really want to delete ?")){
			
			param.id = cls.id;
			param.pageIndex = $scope.pageIndex;
			param.pageSize = $scope.pageSize;
			
			$http({
				method:'POST',
				url:'/mvc/class/delete',
				data:$.param(param),
				headers : {'Content-Type':'application/x-www-form-urlencoded'}
			}).success(
				function(data, status, headers, config){
					$scope.ClassList = data.details;
					$scope.itemsLength = data.count;
				}
			);
		}
	};
	
	$scope.range = function() {
		var pageCount = Math.ceil($scope.itemsLength/$scope.pageSize);
		
		var range = new Array();
		
		for(var i=1; i <= pageCount; i++){
			range[i-1] = i;
		}
		
		return range;
	}
	
	$scope.prevPage = function() { 
		if ($scope.pageIndex > 1) {
		$scope.pageIndex--; }
		
		var param= {};
		param.pageIndex = $scope.pageIndex;
		param.pageSize = $scope.pageSize;
		
		$http(
				{
					method:'POST',
					url:'/mvc/class/listdata',
					data:$.param(param),
					headers : {'Content-Type':'application/x-www-form-urlencoded'}
				}).success(
					function(data, status, headers, config){
						$scope.ClassList=data.details;
						$scope.pageIndex = data.pageIndex;
						$scope.itemsLength = data.count;
					}
				);
	};

	$scope.nextPage = function() {
		if ($scope.pageIndex < $scope.pageCount()) {
		$scope.pageIndex++; }
		
		var param= {};
		param.pageIndex = $scope.pageIndex;
		param.pageSize = $scope.pageSize;
		
		$http(
				{
					method:'POST',
					url:'/mvc/class/listdata',
					data:$.param(param),
					headers : {'Content-Type':'application/x-www-form-urlencoded'}
				}).success(
					function(data, status, headers, config){
						$scope.ClassList=data.details;
						$scope.pageIndex = data.pageIndex;
						$scope.itemsLength = data.count;
					}
				);
	};
	
	$scope.setPage = function(n){
		$scope.pageIndex = n;
		
		var param= {};
		param.pageIndex = $scope.pageIndex;
		param.pageSize = $scope.pageSize;
		
		$http(
				{
					method:'POST',
					url:'/mvc/class/listdata',
					data:$.param(param),
					headers : {'Content-Type':'application/x-www-form-urlencoded'}
				}).success(
					function(data, status, headers, config){
						$scope.ClassList=data.details;
						$scope.pageIndex = data.pageIndex;
						$scope.itemsLength = data.count;
					}
				);
	}

	$scope.prevPageDisabled = function() {
		return $scope.pageIndex == 1 ? "disabled" : "";
	};

	$scope.nextPageDisabled = function() {
		return $scope.pageIndex >= $scope.pageCount() ? "disabled" : "";
	} 
	
	$scope.pageCount = function() {
		$scope.totalPage = Math.ceil($scope.itemsLength/$scope.pageSize);
		return $scope.totalPage;
	};
	
}]);